package disjoint.set;

/**
 * Created by root on 4/5/17.
 */
public class DisjSets {

    private int[] s;

    public DisjSets(int numElements)
    {
        s=new int[numElements];
        for(int i=0;i<s.length;i++)
            s[i]=-1;
    }
    /**
     * @param root1  the root of set1
     * @param root2  the root of set2
     */
    public void union(int root1,int root2)
    {
        s[root2]=root1;
    }
    /**
     *
     * @param x the element being searched for
     * @return the set containing x
     */
    public int find(int x)
    {
        if(s[x]<0)
            return x;
        else
            return find(s[x]);
    }
}

